﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Kaiser.Extensions;
using System.Web.Script.Serialization;

namespace Kaiser.Models
{
    [Serializable]
    public class DataGrid
    {
        [ScriptIgnore]
        public DataTable TableResult { get; set; }
        public int total
        {
            get;
            set;
        }

        public virtual string ToJsonString()
        {
            JavaScriptSerializer json = new JavaScriptSerializer();
            var obj = new { rows = TableResult.ToJson(), total = total };
            return json.Serialize(obj);
        }



    }

    public class DataGrid<T> : DataGrid
    {
        private readonly IList<T> _list;
        private readonly PagingInfo _pageinfo;

        public DataGrid()
        {
        }

        public DataGrid(IList<T> list, PagingInfo pageinfo)
        {
            _list = list;
            _pageinfo = pageinfo;
            this.total = pageinfo.TotalCount;
            //throw new NotImplementedException();
        }

        public IList<T> rows
        {
            get
            {
                if (TableResult==null)
                {
                    return _list;
                }
                return TableResult.ToList<T>();
            }
        }

        public virtual string ToJsonString()
        {
            JavaScriptSerializer json = new JavaScriptSerializer();
            return json.Serialize(this);
        }

    }
}
